<template>
<div>
    <el-dialog :visible.sync="errorPush.dialogErrorPush" size="tiny" title="异常推送设置">
        <el-form label-width="120px" v-loading="loading">
            <el-row :gutter="30">
                <el-row>
                    <el-form-item label="异常推送开关">
                        <el-switch v-model="pushSwitch" active-text="按月付费" inactive-text="按年付费" class="pushSwitch"></el-switch>
                    </el-form-item>
                </el-row>
                <div v-if="pushSwitch">
                    <el-row>
                        <el-form-item label="异常推送时间"></el-form-item>
                    </el-row>
                    <el-row :span="23">
                        <el-col :span="3" :offset="1" class="lineHeight">
                            <el-radio v-model="radio" label="1">每日</el-radio>
                        </el-col>
                        <el-col :span="17">
                            <el-time-select 
                            :editable='false'
                            v-model="pushTimeDay"
                            :picker-options="{
                                start: sClassStartTime,
                                step: '00:10',
                                end: sClassEndTime
                            }"
                            placeholder="请选择推送时间">
                            </el-time-select>
                        </el-col>
                    </el-row>
                    <el-row class="rowTop">
                        <el-col :span="3" :offset="1">
                            <el-radio v-model="radio" label="2">每周</el-radio>
                        </el-col>
                        <el-col :span="20">
                            <el-checkbox v-model="item.boolean" v-for="(item,index) in pushTimeList" :key="index">{{item.name}}</el-checkbox>
                        </el-col>
                        <el-col :span="17" class="time-select">
                            <el-time-select 
                            :editable='false'
                            v-model="pushTimeWeek"
                            :picker-options="{
                                start: sClassStartTime,
                                step: '00:10',
                                end: sClassEndTime
                            }"
                            placeholder="请选择推送时间">
                            </el-time-select>
                        </el-col>
                    </el-row>
                </div>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <p class="btn">
            <el-button @click="resetFormdelDialogAddTea">关 闭</el-button>
            <el-button type="primary" @click="addVersion" :loading="loadingBtn">保 存</el-button>
            </p>
        </div>
    </el-dialog>
</div>
</template>
<script>
import { listCronExpress, listExceptionPush, updateExceptionPush} from 'api/equipmentMange';
import { mapGetters } from 'vuex';
export default {
    props: {
        errorPush: {
            type: Object,
            required: true
        }
    },
    data() {
        return {
            loading: false,
            sClassStartTime: '',
            sClassEndTime: '',
            sStartTime: '',
            loadingBtn: false,
            pushSwitch: true,
            pushTimeWeek: '',
            pushTimeDay: '',
            radio: '1',
            pushTimeList: [
                {
                    name: '周一',
                    boolean: false
                },
                {
                    name: '周二',
                    boolean: false
                },
                {
                    name: '周三',
                    boolean: false
                },
                {
                    name: '周四',
                    boolean: false
                },
                {
                    name: '周五',
                    boolean: false
                },
                {
                    name: '周六',
                    boolean: false
                },
                {
                    name: '周日',
                    boolean: false
                }
            ]
        }
    },
    computed: {
        ...mapGetters([
            'schoolId'
        ])
    },
    created() {
        // this.getExpress();
        // this.getExceptionPush();
    },
    methods: {
        addVersion(version) {
            
            let status = this.pushSwitch ? '0' : '1';
            let weekEveryDay = null;
            let time = null;
            if (this.radio=='2') {
                if (!this.pushTimeWeek) {
                    this.$message({
                        type: 'error',
                        message: '请设置每周推送时间'
                    });
                    return '';
                }
                time = this.pushTimeWeek+':00';
                let weekDay = '';
                let weekDayList = [];
                this.pushTimeList.forEach((item, index) => {
                    if (item.boolean) {
                        weekDay += index+1 + ',';
                        weekDayList.push(index+1);
                    }
                })
                if (weekDayList.length==7) {
                    weekEveryDay = '1/7';
                } else {
                    weekEveryDay = weekDay.substring(0, weekDay.length-1);
                }
            } else {
                if (!this.pushTimeDay) {
                    this.$message({
                        type: 'error',
                        message: '请设置每日推送时间'
                    });
                    return '';
                }
                time = this.pushTimeDay+':00';
                weekEveryDay = '';
            }
            this.loadingBtn = true;
            updateExceptionPush(this.schoolId, status, weekEveryDay, time).then(res => {
                this.loadingBtn = false;
                this.$message({
                    type: 'success',
                    message: '推送设置成功'
                })
                this.delDialogAddTea();
                this.$emit('success', res)
            }).catch(() => {
                this.loadingBtn = false;
            })
        },
        getExpress() {
            listCronExpress(this.schoolId).then(res => {
                this.sClassStartTime = res.data.content.sClassStartTime.substr(0, 5);
                this.sClassEndTime = res.data.content.sClassEndTime.substr(0, 5);
                this.sStartTime = res.data.content.sClassStartTime.substr(0, 5);
                this.pushTimeDay = res.data.content.sClassStartTime.substr(0, 5);
                this.pushTimeWeek = res.data.content.sClassStartTime.substr(0, 5);
            })
        },
        getExceptionPush() {
            this.loading = true;
            listExceptionPush(this.schoolId).then(res => {
                this.loading = false;
                this.pushSwitch = res.data.content.status == '0' ? true : false;
                if (res.data.content.cron) {
                    this.radio = '2';
                    if (res.data.content.cron == '1/7') {
                        res.data.content.cron = '1,2,3,4,5,6,7';
                    }
                    res.data.content.cron.split(',').forEach((item) => {
                        this.pushTimeList[item-1].boolean = true;
                    })
                    this.pushTimeWeek = res.data.content.cronDesc.substr(0, 5);
                } else {
                    this.radio = '1';
                    this.pushTimeDay = res.data.content.cronDesc.substr(0, 5);
                }
            }).catch(() => {
                this.loading = false;
            })
        },
        handleClose() {
            this.$refs.version.resetFields();
            this.delDialogAddTea();
        },
        getNumber() {
            if (this.configureit.relayType=='0') {
                this.getPanels();
            } else {
                this.getlistNumber();
            }
        },
        // 取消按钮 
        resetFormdelDialogAddTea(dataForm) {
            this.delDialogAddTea();
        },
        // 还原清空弹窗信息
        delDialogAddTea() {
            this.loadingBtn = false;
            this.pushSwitch = false;
            this.errorPush.dialogErrorPush = false;
            this.radio = '1';
            this.pushTimeDay = this.sStartTime;
            this.pushTimeWeek = this.sStartTime;
            this.pushTimeList=[
                {
                    name: '周一',
                    boolean: false
                },
                {
                    name: '周二',
                    boolean: false
                },
                {
                    name: '周三',
                    boolean: false
                },
                {
                    name: '周四',
                    boolean: false
                },
                {
                    name: '周五',
                    boolean: false
                },
                {
                    name: '周六',
                    boolean: false
                },
                {
                    name: '周日',
                    boolean: false
                }
            ];
            
        }
    }
}
</script>
<style lang="scss" scoped>
    .el-form{
        padding-left:8px;
    }
    .pushSwitch{
        margin-left:25px;
    }
    .lineHeight{
        line-height:36px;
    }
    .rowTop{
        margin-top:30px;
        margin-bottom:30px;
    }
    .time-select{
        margin-top:20px;
    }
    .dialog-footer{
        margin-bottom:15px;
    }
</style>


